/**
 *
 * @author Captain Normality
 */
package xmlparser;

import java.io.CharArrayWriter;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class BuySellParser extends DefaultHandler{
    
    private OrderHandler buyOrders = new OrderHandler();
    private OrderHandler sellOrders = new OrderHandler();
    private String itemName;
    
    private BuySellOrder currOrder;
    private OrderHandler currHandler;
    
    private CharArrayWriter contents = new CharArrayWriter();

    @Override
    public void startElement(String namespaceURI, 
            String localName, 
            String qName, 
            Attributes attr) throws SAXException
    {
        contents.reset();
        
        switch (localName) {
            case "order":
                currOrder = new BuySellOrder();
                break;
            case "sell_orders":
                currHandler = sellOrders;
                break;
            case "buy_orders":
                currHandler = buyOrders;
                break;
        }
                
    }
    
    @Override
    public void characters( char[] ch, int start, int length )
            throws SAXException 
    {
        contents.write( ch, start, length );
    }
    
    @Override
    public void endElement(String namespaceURI, 
            String localName, 
            String qName) throws SAXException
    {
        switch (localName) {
            case "region":
                currOrder.region = Integer.valueOf(contents.toString().trim()).intValue();
                break;
            case "station":
                currOrder.station = Integer.valueOf(contents.toString().trim()).intValue();
                break;
            case "station_name":
                currOrder.stationName = contents.toString();
                break;
            case "security":
                currOrder.security = Double.valueOf(contents.toString().trim()).doubleValue();
                break;
            case "range":
                currOrder.range = Double.valueOf(contents.toString().trim()).doubleValue();
                break;
            case "price":
                currOrder.price = Double.valueOf(contents.toString().trim()).doubleValue();
                break;
            case "vol_remain":
                currOrder.volume = Integer.valueOf(contents.toString().trim()).intValue();
                break;
            case "min_volume":
                currOrder.minVolume = Integer.valueOf(contents.toString().trim()).intValue();
                break;
            case "expires":
                currOrder.expires = contents.toString();
                break;
            case "reported_time":
                currOrder.reported = contents.toString();
                break;
            case "order":
                currHandler.addOrder(currOrder);
                break;
            case "itemname":
                itemName = contents.toString();
                break;
        }
    }
    
    public String getItemName()
    {
        return itemName;
    }
    
    public OrderHandler getBuyOrders()
    {
        return buyOrders;
    }
    
    public OrderHandler getSellOrders()
    {
        return sellOrders;
    }
    
}
